package com.content.management.support;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.content.management.common.BaseEntity;
import com.content.management.common.BaseTimeEntity;
import com.google.common.collect.Lists;
import org.junit.jupiter.api.Test;

/**
 * @author xinghu
 * @version 1.0
 * @date 2020/2/10 12:22 上午
 */
public class GeneratorServiceEntity {

	String dbUrl =
			"jdbc:mysql://127.0.0.1:3306/content_management_platform?characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&serverTimezone=Asia/Shanghai";
	String username = "root";
	String password = "huxing";

	@Test
	public void generateCode() {
		String packageName = "com.content.management";
		generateByTables(packageName,
				"bus_publish_content",
				"dictionary_data", "dictionary_directory", "manage_resources", "manage_role", "manage_role_resources", "manage_user_info", "manage_user_role");
	}

	private void generateByTables(
			String packageName, String... tableNames) {

		String projectPath = System.getProperty("user.dir");
		new AutoGenerator()
				.setGlobalConfig(new GlobalConfig()
						.setOutputDir(projectPath + "/src/main/java/")
						.setServiceName("%sService")
						.setMapperName("%sDao")
						.setActiveRecord(false)
						.setBaseColumnList(true)
						.setEnableCache(false)
						.setBaseResultMap(true)
						.setIdType(IdType.AUTO)
						.setAuthor("xinghu")
						.setFileOverride(true)
						.setOpen(false)
						.setSwagger2(true))
				.setDataSource(new DataSourceConfig()
						.setDbType(DbType.MYSQL)
						.setUrl(dbUrl)
						.setUsername(username)
						.setPassword(password)
						.setDriverName("com.mysql.cj.jdbc.Driver")
				)
				.setStrategy(new StrategyConfig()
						.setColumnNaming(NamingStrategy.underline_to_camel)
						.setSuperEntityClass(BaseEntity.class, NamingStrategy.underline_to_camel)
						.setCapitalMode(true)
						.setEntityLombokModel(true)
						.setSkipView(true)
						.setNaming(NamingStrategy.underline_to_camel)
						.setInclude(tableNames)
						.setRestControllerStyle(true)
						.setEntityTableFieldAnnotationEnable(true))
				.setPackageInfo(new PackageConfig()
						.setParent(packageName)
						.setController("controller")
						.setMapper("dao")
						.setEntity("domain.entity"))
				.setCfg(new InjectionConfig() {
					@Override
					public void initMap() {
					}
				}.setFileOutConfigList(Lists.newArrayList(new FileOutConfig("/templates/mapper.xml.vm") {
					@Override
					public String outputFile(TableInfo tableInfo) {
						return projectPath + "/src/main/resources/mapper/" + tableInfo.getEntityName() + "Mapper"
								+ StringPool.DOT_XML;
					}
				}))).setTemplate(new TemplateConfig().setXml(null))
				.execute();
	}
}
